﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Hangout.Client.Search
{
    public partial class Search : PhoneApplicationPage
    {
        string text="";
        MeetupService.Meetup CurrentHangout;
        
        
        public Search()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //search btn clicked :)

                text = SearchTB.Text;
                if (String.IsNullOrWhiteSpace(text))
                {
                    MessageBox.Show("Please enter search text");
                    return;
                }
                HangoutList.Hangouts.Clear();

                LoadingPG.Visibility = System.Windows.Visibility.Visible;
                Services.MeetupServiceClient.SearchHangoutsCompleted += new EventHandler<MeetupService.SearchHangoutsCompletedEventArgs>(client_SearchHangoutsCompleted);
                Services.MeetupServiceClient.SearchHangoutsAsync(Settings.Settings.UserID, text, HangoutList.Take, HangoutList.Skip, Settings.Settings.FacebookAccessToken);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
           

        }

        void client_SearchHangoutsCompleted(object sender, MeetupService.SearchHangoutsCompletedEventArgs e)
        {
            Services.MeetupServiceClient.SearchHangoutsCompleted -= new EventHandler<MeetupService.SearchHangoutsCompletedEventArgs>(client_SearchHangoutsCompleted);
            try
            {
                LoadingPG.Visibility = System.Windows.Visibility.Collapsed;
                HangoutList.MoreHangoutsBtn.Visibility = System.Windows.Visibility.Visible;
                if (e.Error == null)
                {
                   
                    if (e.Result.Count > 0)
                    {
                        HangoutList.Hangouts.AddRange(e.Result.ToList());
                    }
                    HangoutList.RefreshList();
                }
                else
                {
                    MessageBox.Show(ErrorText.Description, ErrorText.Caption, MessageBoxButton.OK);
                }
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                AttachEvents();

            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                //navigate to dashboard
                NavigateToDashboard();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void NavigateToDashboard()
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    if (NavigationService.CanGoBack)
                    {
                        var uri = NavigationService.BackStack.First().Source;

                        if (uri.ToString() == "/MainPage.xaml")
                        {
                            NavigationService.GoBack();
                        }
                        else
                        {
                            NavigationService.Navigate(new Uri(Navigation.Dashboard, UriKind.RelativeOrAbsolute));
                        }
                    }
                    else
                    {
                        NavigationService.Navigate(new Uri(Navigation.Dashboard, UriKind.RelativeOrAbsolute));
                    }

                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void AttachEvents()
        {
            try
            {
                HangoutList.ShowMoreButton = true;
                HangoutList.ShoutNewHangoutSelected += new EventHandler(HangoutList_ShoutNewHangoutSelected);
                HangoutList.ListSV.Height = 575;
                HangoutList.StatusTextSeletcted += new Controls.Hangouts.HangoutList.StatusTextSelectedHelper(HangoutList_StatusTextSeletcted);
                HangoutList.HangoutSelected += new Controls.Hangouts.HangoutList.HangoutSelectedHelper(HangoutList_HangoutSelected);
                HangoutList.ProfileNameSeletcted += new Controls.Hangouts.HangoutList.ProfileNameSelectedHelper(HangoutList_ProfileNameSeletcted);
                HangoutList.ProfilePicSeletcted += new Controls.Hangouts.HangoutList.ProfilePicSelectedHelper(HangoutList_ProfilePicSeletcted);
                HangoutList.PinSeletcted += new Controls.Hangouts.HangoutList.PinSelectedHelper(HangoutList_PinSeletcted);
                HangoutList.UnpinSeletcted += new Controls.Hangouts.HangoutList.UnPinSelectedHelper(HangoutList_UnpinSeletcted);
                HangoutList.MoreHangoutClicked += new Controls.Hangouts.HangoutList.MoreHangoutClickedHelper(HangoutList_MoreHangoutClicked);
                LoadingPG.Visibility = System.Windows.Visibility.Collapsed;
                HangoutList.MoreHangoutsBtn.Visibility = System.Windows.Visibility.Collapsed;
                
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void DetachEvents()
        {
            try
            {
                HangoutList.ShoutNewHangoutSelected -= HangoutList_ShoutNewHangoutSelected;
                HangoutList.StatusTextSeletcted -= HangoutList_StatusTextSeletcted;
                HangoutList.HangoutSelected -= HangoutList_HangoutSelected;
                HangoutList.ProfileNameSeletcted -= HangoutList_ProfileNameSeletcted;
                HangoutList.ProfilePicSeletcted -= HangoutList_ProfilePicSeletcted;
                HangoutList.PinSeletcted -= HangoutList_PinSeletcted;
                HangoutList.UnpinSeletcted -= HangoutList_UnpinSeletcted;
                HangoutList.MoreHangoutClicked -= HangoutList_MoreHangoutClicked;
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }


        void HangoutList_ShoutNewHangoutSelected(object sender, EventArgs e)
        {
            try
            {
                GotoShoutOutPage();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        
        private void GotoShoutOutPage()
        {
            try
            {
                //shout out
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.InitiateHangout, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

       

        
        

       
        void HangoutList_StatusTextSeletcted(MeetupService.Meetup data)
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Hangout + "?id=" + data.HangoutID, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void HangoutList_MoreHangoutClicked(int take, int skip)
        {
            try
            {
                HangoutList.MoreHangoutsBtn.Visibility = System.Windows.Visibility.Collapsed;
                LoadingPG.Visibility = System.Windows.Visibility.Visible;
                Services.MeetupServiceClient.SearchHangoutsCompleted += new EventHandler<MeetupService.SearchHangoutsCompletedEventArgs>(client_SearchHangoutsCompleted);
                Services.MeetupServiceClient.SearchHangoutsAsync(Settings.Settings.UserID, text, HangoutList.Take, HangoutList.Skip, Settings.Settings.FacebookAccessToken);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void HangoutList_PinSeletcted(MeetupService.Meetup data)
        {
            try
            {
                Services.MeetupServiceClient.PinHangoutCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_PinHangoutCompleted);
                CurrentHangout = data;
                Services.MeetupServiceClient.PinHangoutAsync(Settings.Settings.UserID, data.HangoutID, Settings.Settings.FacebookAccessToken);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_PinHangoutCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Services.MeetupServiceClient.PinHangoutCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_PinHangoutCompleted);
            try
            {
                if (e.Error == null)
                {
                    HangoutList.DisplayUnPinImage(CurrentHangout);
                }
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void HangoutList_ProfilePicSeletcted(MeetupService.Meetup data)
        {
            try
            {
                NavigateToViewProfile(data.User.UserID);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
            
        }

        void HangoutList_ProfileNameSeletcted(MeetupService.Meetup data)
        {
            try
            {

                NavigateToViewProfile(data.User.UserID);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
            
        }

        

        void HangoutList_HangoutSelected(MeetupService.Meetup e)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Hangout + "?id=" + e.HangoutID, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void HangoutList_UnpinSeletcted(MeetupService.Meetup data)
        {
            try
            {
                
                Services.MeetupServiceClient.UnPinHangoutCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UnPinHangoutCompleted);
                CurrentHangout = data;
                Services.MeetupServiceClient.UnPinHangoutAsync(Settings.Settings.UserID, data.HangoutID, Settings.Settings.FacebookAccessToken);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_UnPinHangoutCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Services.MeetupServiceClient.UnPinHangoutCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_UnPinHangoutCompleted);
            try
            {
                if (e.Error == null)
                {
                    HangoutList.DisplayPinImage(CurrentHangout);
                }
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        

       

        private void NavigateToViewProfile(int id)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.ViewProfile + "?id=" + id, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            Button_Click(new object(), new RoutedEventArgs());
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            DetachEvents();
            base.OnNavigatedFrom(e);
        }

       
    }
}